package com.template.project.config;

import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * web相关配置
 *
 * @author xs
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    private final List<HandlerInterceptor> interceptorList;

    @Autowired
    public MvcConfig(List<HandlerInterceptor> interceptorList) {
        this.interceptorList = interceptorList;
    }

    @Override
    public void addInterceptors(@NotNull InterceptorRegistry registry) {
        if (CollectionUtils.isEmpty(interceptorList)) {
            return;
        }
        interceptorList.forEach(e -> registry.addInterceptor(e).addPathPatterns("/**"));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/preview/**");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swaggerApi").setViewName("redirect:swagger-ui.html");
    }
}
